package com.example.common.cqsupervision.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.example.common.cqsupervision.dto.req.Head;
import com.example.common.cqsupervision.dto.req.ReqBody;
import com.example.common.support.ApiException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class HttpHelp {
    @Autowired
    private RestTemplate restTemplate;
    
    /**
     * 提交重庆监管
     * @param url 监管地址
     * @param head 请求头对象
     * @param parm 请求体对象
     * @return
     */
    public String doSupervisionPost(String url, Sgin head, Map<String, Object> parm){   
        
        // String inputJson = JSONObject.toJSONString(parm);
        
        // HttpHeaders headers = new HttpHeaders();
        // headers.add("x-rb-key", head.getXRbKey());
        // headers.add("x-rb-timestamp", head.getXRbTimestamp().toString());
        // headers.add("x-rb-requuid", head.getXRbRequuid());
        // headers.add("x-rb-sign", head.doSign());
        // headers.setContentType(MediaType.APPLICATION_JSON);
        // HttpEntity<String> req = new HttpEntity<>(inputJson, headers);
        // try {
        //     String dataMap = restTemplate.postForObject(url, req, String.class);
        //     return dataMap;
        // } catch (Exception e) {
        //     //TODO: handle exception
        // }
        // return "";
        
        return JSONObject.toJSONString(parm);
    }

    private String getClientIP(HttpServletRequest request) {   
        String ip = request.getHeader("x-forwarded-for");   
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getHeader("Proxy-Client-IP");   
        }   
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getHeader("WL-Proxy-Client-IP");   
      
        }   
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getRemoteAddr();   
        }   
        return ip;   
    } 

    public String getMac(HttpServletRequest request){
        String ip = getClientIP(request);
        String mac = "";
        try {
	    Process p = Runtime.getRuntime().exec("arp -n");
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
			p.waitFor();
            boolean flag = true;
			String ipStr = "(" + ip + ")";
            while(flag) {
                String str = input.readLine();
                if (str != null) {
					if (str.indexOf(ipStr) > 1) {
                        int temp = str.indexOf("at");
                        mac = str.substring(temp + 3, temp + 20);
                        break;
                    }
                } else
                flag = false;
            }
        } catch (IOException | InterruptedException e) {
            throw new ApiException(1000, e.toString());
        }
        return mac;
    }
}
